package com.xbai.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xbai.goods.dao.StockBackMapper;
import com.xbai.goods.pojo.StockBack;
import com.xbai.goods.service.StockBackService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @desc StockBack业务层实现类
 * @author jxbai
 * @date 2021/1/28
 */
@Service
@RequiredArgsConstructor
public class StockBackServiceImpl implements StockBackService {

    private final StockBackMapper stockBackMapper;

    /**
     * StockBack条件+分页查询
     * @param stockBack {@link StockBack}
     * @param page 页码
     * @param size 页大小
     * @return {@link PageInfo< StockBack >} 分页结果
     */
    @Override
    public PageInfo<StockBack> findPage(StockBack stockBack, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(stockBack);
        //执行搜索
        return new PageInfo<>(stockBackMapper.selectByExample(example));
    }

    /**
     * StockBack分页查询
     * @param page 页码
     * @param size 页大小
     * @return {@link PageInfo< StockBack >}
     */
    @Override
    public PageInfo<StockBack> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<>(stockBackMapper.selectAll());
    }

    /**
     * StockBack条件查询
     * @param stockBack {@link StockBack}
     * @return {@link List < StockBack >}
     */
    @Override
    public List<StockBack> findList(StockBack stockBack){
        //构建查询条件
        Example example = createExample(stockBack);
        //根据构建的条件查询数据
        return stockBackMapper.selectByExample(example);
    }


    /**
     * StockBack构建查询对象
     * @param stockBack {@link StockBack}
     * @return {@link Example} 条件对象
     */
    private Example createExample(StockBack stockBack){
        Example example=new Example(StockBack.class);
        Example.Criteria criteria = example.createCriteria();
        if(stockBack!=null){
            // 订单id
            if(!StringUtils.isEmpty(stockBack.getOrderId())){
                    criteria.andEqualTo("orderId",stockBack.getOrderId());
            }
            // SKU的id
            if(!StringUtils.isEmpty(stockBack.getSkuId())){
                    criteria.andEqualTo("skuId",stockBack.getSkuId());
            }
            // 回滚数量
            if(!StringUtils.isEmpty(stockBack.getNum())){
                    criteria.andEqualTo("num",stockBack.getNum());
            }
            // 回滚状态
            if(!StringUtils.isEmpty(stockBack.getStatus())){
                    criteria.andEqualTo("status",stockBack.getStatus());
            }
            // 创建时间
            if(!StringUtils.isEmpty(stockBack.getCreateTime())){
                    criteria.andEqualTo("createTime",stockBack.getCreateTime());
            }
            // 回滚时间
            if(!StringUtils.isEmpty(stockBack.getBackTime())){
                    criteria.andEqualTo("backTime",stockBack.getBackTime());
            }
        }
        return example;
    }

    /**
     * 删除StockBack
     * @param id 主键id
     */
    @Override
    public void delete(String id){
        stockBackMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改StockBack数据
     * @param stockBack {@link StockBack}
     */
    @Override
    public void update(StockBack stockBack){
        stockBackMapper.updateByPrimaryKey(stockBack);
    }

    /**
     * 新增StockBack
     * @param stockBack {@link StockBack}
     */
    @Override
    public void add(StockBack stockBack){
        stockBackMapper.insert(stockBack);
    }

    /**
     * 根据ID查询StockBack
     * @param id 主键id
     * @return {@link StockBack}
     */
    @Override
    public StockBack findById(String id){
        return  stockBackMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有StockBack
     * @return {@link List < StockBack >}
     */
    @Override
    public List<StockBack> findAll() {
        return stockBackMapper.selectAll();
    }
}
